package com.han.controllers;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;

import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.han.bom.Exam;
import com.han.bom.ExamResponse;
import com.han.bom.MultipleChoice;
import com.han.bom.Question;
import com.han.bom.Student;
import com.han.dao.concrete.product.AnswerDAO;
import com.han.dao.concrete.product.ExamDAO;
import com.han.dao.concrete.product.ExamResponseDAO;
import com.han.dao.concrete.product.QuestionDAO;

/**
 * Servlet implementation class StudentSearchServlet
 */
@WebServlet("/Student/StudentSearchServlet")
public class StudentSearchServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public StudentSearchServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		HttpSession session = request.getSession();
		
		Student student =  (Student) session.getAttribute("student");
		String key = request.getParameter("searchKey");
		ExamDAO examdao = new ExamDAO();
		ExamResponseDAO examresponsedao = new ExamResponseDAO();
		
		ArrayList<ExamResponse> realMissed = new ArrayList<ExamResponse>();
		ArrayList<ExamResponse> realTaken = new ArrayList<ExamResponse>();
		ArrayList<ExamResponse> practice = new ArrayList<ExamResponse>();
		ArrayList<Exam> realNotYetTaken = new ArrayList<Exam>();
		
		try {
			realMissed = examresponsedao.retrieveMissedRealExamsByKeyword(key, student.getUserID());
			realTaken = examresponsedao.retrieveRealExamsTakenByKeyword(key, student.getUserID());
			realNotYetTaken = examdao.retrieveRealExamsNotTakenByKeyword(key, student.getUserID());
			practice = examresponsedao.retrievePracticeExamsTakenByKeyword(key, student.getUserID());
			
			for(ExamResponse exam:realMissed){
				constructExamResponse(exam);
			}
			for(ExamResponse exam:realTaken){
				constructExamResponse(exam);
			}
			for(Exam exam:realNotYetTaken){
				constructExam(exam);
			}
			for(ExamResponse exam:practice){
				constructExamResponse(exam);
			}
			
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		request.setAttribute("realMissed", realMissed);
		request.setAttribute("realTaken", realTaken);
		request.setAttribute("practice", practice);
		request.setAttribute("realNotYetTaken", realNotYetTaken);
		
		RequestDispatcher rd = request.getRequestDispatcher("Search.jsp");
		
		rd.forward(request, response);
	}

	private void constructExamResponse(ExamResponse examResponse){
		
		QuestionDAO questiondao = new QuestionDAO();
		Exam exam = examResponse.getExam();
		
		try {
			exam.setQuestions(questiondao.retrieveQuestionsbyExam(exam.getExamID()));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	private void constructExam(Exam exam){
			
		QuestionDAO questiondao = new QuestionDAO();
		AnswerDAO answerdao = new AnswerDAO();
		
		try {
			exam.setQuestions(questiondao.retrieveQuestionsbyExam(exam.getExamID()));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		for(Question question: exam.getQuestions()){
			ArrayList<String> choices = null;
			try {
				choices = answerdao.retrieveAnswersByQuestion(question.getQuestionID());
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (NamingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			question.setAnswer(choices.get(0));
			
			if(question instanceof MultipleChoice){
				Collections.sort(choices);
			}
			else{
				Collections.sort(choices);
				Collections.reverse(choices);
			}
		}
			
	}
	
}
